//
//  ViewController.m
//  Ble4.0-SrialPort
//
//  Created by Mike on 15/7/7.
//  Copyright (c) 2015年 MaxWiLL. All rights reserved.
//
#define kUUID @"ffe1"

#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate>

@property (strong, nonatomic) IBOutlet UITextField *inputTextField;


@property (strong, nonatomic) CBCentralManager *manager;

@property (strong, nonatomic) NSMutableArray *peripherals;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setupKeyboardNotification];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  懒加载初始化CBcentralManager
 */
- (CBCentralManager *)manager{
    if (!_manager) {
        self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
    }
    return _manager;
}

- (NSMutableArray *)peripherals{
    if (!_peripherals) {
        self.peripherals = [NSMutableArray array];
    }
    return _peripherals;
}


#pragma mark - CBCentralManagerDelegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    NSLog(@" %d | %s ",__LINE__,__FUNCTION__);
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
    
    NSLog(@" %d | %s | %@ | %@ | %@",__LINE__,__FUNCTION__,peripheral,advertisementData,RSSI);
    [self.peripherals addObject:peripheral];
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
    peripheral.delegate = self;
    [peripheral discoverServices:@[[CBUUID UUIDWithString:kUUID],[CBUUID UUIDWithString:@"ffe0"]]];
    NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,peripheral);
}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
    NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,error);
}


#pragma mark - CBPeripheralDelegate


- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,peripheral);
    if (error) {
        NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,error);
        return;
    }
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kUUID]] forService:service];

    }
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
    if (error) {
        NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,error );
        return ;
    }
    for (CBCharacteristic *characteristic in service.characteristics) {
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kUUID]]) {
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
    }
}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if (error) {
        NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,error);
        return;
    }
    
    if (characteristic.isNotifying) {
        NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,characteristic);
    }else{
        NSLog(@" %d | %s | %@ | %@",__LINE__,__FUNCTION__,characteristic,@"disconnected");
    }
    
}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if (error) {
        NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,error);
        return;
    }
    
    NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,characteristic);
}

#pragma mark - 键盘监听
/**
 *  监听键盘是否弹出，修改页面frame，是输入框不背遮住
 */
- (void)setupKeyboardNotification{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDismiss:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSValue *frameVale = userInfo[UIKeyboardFrameEndUserInfoKey];
    CGRect frame = self.view.frame;
    frame.size.height = [frameVale CGRectValue].origin.y;
    self.view.frame = frame;
    
}



- (void)keyboardWillDismiss:(NSNotification *)notifcation{
    self.view.frame = [UIScreen mainScreen].bounds;
}


#pragma mark - UI事件处理
- (IBAction)history:(id)sender {
    NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,self.peripherals );
    [self.manager connectPeripheral:self.peripherals.firstObject options:nil];
}

- (IBAction)settingAction:(id)sender {
    CBUUID *uuid = [CBUUID UUIDWithString:@"ffe0"];
    NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,uuid.UUIDString);
    [self.manager scanForPeripheralsWithServices:@[uuid] options:nil];
}

- (IBAction)swipeToRightAction:(id)sender {
    NSLog(@" %d | %s ",__LINE__,__FUNCTION__);
}
- (IBAction)swipeToLeftAction:(id)sender {
    NSLog(@" %d | %s ",__LINE__,__FUNCTION__);
}
- (IBAction)swipeUpAction:(id)sender {
    NSLog(@" %d | %s ",__LINE__,__FUNCTION__);
    [_inputTextField becomeFirstResponder];
    
}
- (IBAction)swipeDownAction:(id)sender {
    NSLog(@" %d | %s ",__LINE__,__FUNCTION__);    [_inputTextField resignFirstResponder];
}

@end
